package com.eastmoney.emis.edda.infrastructure.bankname;

import com.eastmoney.emis.edda.infrastructure.bankname.repository.BankRepository;
import com.eastmoney.emis.edda.infrastructure.bankname.repository.MultiLanguageRepository;
import com.eastmoney.emis.utils.common.po.BaseResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @description: Description
 * @projectName: EDDA-HSBC
 * @package: com.eastmoney.emis.edda.infrastructure.bankname
 * @author: 汪程程
 * @date: 2022/10/20 10:12 AM
 * @version: 1.0
 */
@Service
public class BankNameService {

    private static final Logger log = LoggerFactory.getLogger(BankNameService.class);
    @Autowired
    private MultiLanguageRepository repository;

    @Autowired
    private BankRepository bankRepository;

    private Map<String, String> bankCodeNameMap;

    private Map<String, String> englishNameMap;

    public void loadBankMultiLanguageName() {
        bankCodeNameMap = bankRepository.getSupportBankCodeNameMap();
        log.info("loading bank multiLanguage name, size = {}", bankCodeNameMap.size());
    }

    public void initEnglishNameMap() {
        BaseResponse<Map<String, Map<String, String>>> mapBaseResponse = repository.getAll();
        if(mapBaseResponse.getStatus() == 0) {
            if(englishNameMap == null) {
                englishNameMap = new HashMap<>();
            }
            Map<String, String> enMap = mapBaseResponse.getData().getOrDefault("en", new HashMap<>());
            for (String bankName : bankCodeNameMap.values()) {
                englishNameMap.put(bankName, enMap.getOrDefault(bankName, bankName));
            }
        }
    }

    public String getNameByCode(String code) {
        return bankCodeNameMap.get(code);
    }
    public String getEngNameByCode(String code) {
        return getEngName(bankCodeNameMap.get(code));
    }

    public String getEngName(String name) {
        if(englishNameMap == null){
            return name;
        }
        return englishNameMap.getOrDefault(name, name);

    }
}
